package com.huaxin.crm.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.huaxin.crm.util.LayuiUploadFileBean;

/**
 *
 * @版权: Copyright (c) 2016-2017 ****公司技术开发部
 * @author :Steven Liu
 * @E-mail:1661105301@qq.com
 * @版本: 1.0
 * @创建日期: 2017年12月3日 下午3:29:34
 * @ClassName Param2Controller
 * @类描述-Description: TODO
 * @修改记录:
 * @版本: 1.0
 */
@Controller
@RequestMapping("/file")
public class FileUploadController {

	@RequestMapping("/img")
	@ResponseBody
	public LayuiUploadFileBean add(MultipartFile pic, MultipartFile upload,
			MultipartFile fileupload, MultipartFile file) throws IOException {
		String originalFilename = pic.getOriginalFilename();// 源文件名
		String name2 = pic.getName();// 界面上 input name="name值"
		String contentType = pic.getContentType();// mimeType
		InputStream inputStream = pic.getInputStream();
		long size = pic.getSize();
		// 文件名重复问题
		String newFileName = UUID.randomUUID().toString()
				+ originalFilename.substring(originalFilename.lastIndexOf("."));
		// 上传图片
		File parentFile = new java.io.File(
				"F:/develpment/web/huaxin/tomcat/apache-tomcat-7.0.75_learn/apache-tomcat-7.0.75/webapps/files");
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		File uploadPic = new File(parentFile, newFileName);
		// 向磁盘写文件
		pic.transferTo(uploadPic);
		String path = "http://localhost:80/files/" + newFileName;

		LayuiUploadFileBean<Object> layuiUploadFileBean = new LayuiUploadFileBean<>(
				path);

		return layuiUploadFileBean;
	}
}
